package jdmx.library.da;

import java.util.ArrayList;

public class DataTable {
	protected ArrayList<DataColumn> columns;
	protected ArrayList<DataRow> rows;
	protected int columnCount;
	protected int rowCount;
	
	public DataTable() {
		columns = new ArrayList<DataColumn>();
		rows = new ArrayList<DataRow>();
		columnCount = 0;
		rowCount = 0;
	}
	
	public DataColumn addColumn(String columnName, String columnType) {
		DataColumn column = new DataColumn();
		column.table = this;
		column.name = columnName;
		column.type = columnType;
		column.index = columnCount;
		columns.add(column);
		columnCount += 1;
		return column;
	}
	
	public DataRow addRow() {
		DataRow row = new DataRow();
		row.table = this;
		row.cells = new DataCell[columnCount];
		row.index = rowCount;
		rows.add(row);
		rowCount += 1;
		return row;
	}
	
	public DataColumn getColumn(String columnName) {
		DataColumn result = null;
		for (DataColumn column: columns) {
			if (column.name.equalsIgnoreCase(columnName)) {
				result = column;
				break;
			}
		}
		return result;
	}
	
	public DataColumn getColumn(int index) {
		DataColumn result = null;
		for (DataColumn column: columns) {
			if (column.index == index) {
				result = column;
				break;
			}
		}
		return result;
	}
	
	public ArrayList<DataColumn> getColumns() {
		return columns;
	}

	public DataRow getRow(int index) {
		DataRow result = null;
		for (DataRow row: rows) {
			if (row.index == index) {
				result = row;
				break;
			}
		}
		return result;
	}
	
	public ArrayList<DataRow> getRows() {
		return rows;
	}
}
